package com.pomr.controller.synthesize;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pomr.model.synthesize.Linkman;
import com.pomr.model.synthesize.Manufacturer;
import com.pomr.model.sys.User;
import com.pomr.service.synthesize.LxrService;
import com.pomr.util.common.JWT;
import com.pomr.util.common.ResponseData;
import com.pomr.util.page.Page;
import com.pomr.util.page.PageParam;

@Controller
@RequestMapping("/lxr")
public class LxrController {
	@Autowired
	@Qualifier("lxrServiceImpl")
	private LxrService lxrService;
	
	@RequestMapping(value="/inter-search",method=RequestMethod.GET)
	@ResponseBody
	public Page<List<Linkman>> searchLxrs(PageParam param,HttpServletRequest request) throws Exception{
		String page = param.getPage();
		String limit = param.getLimit();
		Map<String, String> params = param.getParams();
		String mid = params.get("mid");//厂商编号
		User user = JWT.findUserByToken(request);
		if(mid==null || mid.equals("")){
			return null;
		}
		return lxrService.searchLxrsByMid(mid, page, limit, user);
	}
	
	@RequestMapping(value="/inter-edit",method=RequestMethod.POST)
	@ResponseBody
	public ResponseData editLxr(@RequestBody Linkman lxr,HttpServletRequest request) throws Exception{
		ResponseData pd = null;
		if(lxr==null || lxr.getId()==null){
			pd = ResponseData.custom("联系人编码未找到");
		}else{
			if(lxrService.editLxr(lxr)){
				pd = ResponseData.ok();
			}else{
				pd = ResponseData.custom("更新失败");
			}
		}
		return pd;
	}
	
	@RequestMapping(value="/inter-add",method=RequestMethod.POST)
	@ResponseBody
	public ResponseData addLxr(@RequestBody Linkman lxr,HttpServletRequest request) throws Exception{
		ResponseData pd = null;
		if(lxr==null || lxr.getManufacturerId()==null){
			pd = ResponseData.custom("对应厂商未找到");
		}else{
			User user = JWT.findUserByToken(request);
			if(lxrService.addLxr(lxr, user)){
				pd = ResponseData.ok();
			}else{
				pd = ResponseData.custom("保存失败");
			}
		}
		return pd;
	}
	
	@RequestMapping(value="/inter-delete",method=RequestMethod.POST)
	@ResponseBody
	public ResponseData addLxr(@RequestBody Map<String,String> map,HttpServletRequest request) throws Exception{
		ResponseData pd = null;
		String ids = map.get("id");
		if(ids==null || ids.trim().equals("")){
			pd = ResponseData.custom("请上传联系人编码");
		}else{
			if(lxrService.deleteLxrs(ids)){
				pd = ResponseData.ok();
			}else{
				pd = ResponseData.custom("删除失败");
			}
		}
		return pd;
	}

}
